
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  logger;

import org.apache.log4j.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import utility.Input;

public class PoolLogger extends org.apache.log4j.Logger 
{
  // It's usually a good idea to add a dot suffix to the fully
  // qualified class name. This makes caller localization to work
  // properly even from classes that have almost the same fully
  // qualified class name as MyLogger, e.g. MyLoggerTest.
  static String FQCN = PoolLogger.class.getName() + ".";
  Layout layout;
  
  String format = "yyyy-MM-dd";
  SimpleDateFormat sdm;  
  String date;
  
  org.apache.log4j.Logger logger;
  FileAppender fa_admin, fa_manager, fa_user;
  
  // It's enough to instantiate a factory once and for all.
  private static PoolLoggerFactory myFactory = new PoolLoggerFactory();
  
  /**
     Just calls the parent constuctor.
   */
  public PoolLogger(String name) {
    super(name);
    //logger = PoolLogger.getLogger(name);
    
    //PropertyConfigurator.configure("logger.properties");
    //Input.init();
    String temp = Input.CONVERSIONPATTERN ;
    layout = new PatternLayout(Input.CONVERSIONPATTERN);
    //logger = PoolLogger.getLogger(name);
    this.setLevel(Level.ALL);
    sdm = new SimpleDateFormat(format);
	date = sdm.format(new Date());
	
	this.setFileAppender();
	
  }
  
  private void setFileAppender()
  {
	  String path = null, dirpath = null;
	  
	  // admin appender
	  try
	  {
		  try
		  {
			  dirpath = Input.LogDir + "admin//";
			  File dirfile = new File(dirpath);
			  if(!dirfile.isDirectory())
				  dirfile.mkdirs();
			  path = dirpath + date +".log";
			  RandomAccessFile raf = new RandomAccessFile(path,"r");
			  dirfile = null;
		  }
		  catch (FileNotFoundException e) 
		  {
			  File file = new File(path);
			  
			  if(file.createNewFile())
				  System.out.println("File created");
			  else
				  System.out.println("File not created");
			  			  
			  file = null;
		  }
		  
		  fa_admin = new FileAppender(layout,path);
	  }
	  catch (IOException e) 
	  {
	  }
	  
	  // manager appender
	  try
	  {
		  try
		  {			  
			  dirpath = Input.LogDir + "manager//";
			  File dirfile = new File(dirpath);
			  if(!dirfile.isDirectory())
				  dirfile.mkdirs();
			  dirfile = null;
			  
			  path = dirpath + date +".log";
			  RandomAccessFile raf = new RandomAccessFile(path,"r");			  
		  }
		  catch (FileNotFoundException e) 
		  {
			  File file = new File(path);
			  
			  if(file.createNewFile())
				  System.out.println("File created");
			  else
				  System.out.println("File not created");
			  			  
			  file = null;
		  }
		  

		  fa_manager = new FileAppender(layout,path);		  
	  }
	  catch (IOException e) 
	  {
	  }
	  
	  
	  try
	  {
		  try
		  {
			  dirpath = Input.LogDir + "user//";
			  File dirfile = new File(dirpath);
			  if(!dirfile.isDirectory())
				  dirfile.mkdirs();
			  dirfile = null;
			  
			  path = dirpath + date +".log";
			  RandomAccessFile raf = new RandomAccessFile(path,"r");
		  }
		  catch (FileNotFoundException e) 
		  {
			  File file = new File(path);
			  
			  if(file.createNewFile())
				  System.out.println("File created");
			  else
				  System.out.println("File not created");
			  
			  file = null;
		  }
		  
		  fa_user = new FileAppender(layout,path);		  
	  }
	  catch (IOException e) 
	  {
	  }  
	  
  }

  @Override
  public void debug(Object message) {
	  if(Input.LOGGER_DEBUG)
	  {		  
		  if(Input.FILE_APPENDER)
			  this.addAppender(fa_admin);
		  if(Input.CONSOLE_APPENDER)
			  this.addAppender(new ConsoleAppender(layout));
		  super.log(FQCN, Level.DEBUG, message, null);
		  this.removeAppender(fa_admin);
	  }
  }
  
  public void trace(Object message) {
	  if(Input.LOGGER_TRACE)
	  {
		  if(Input.FILE_APPENDER)
			  this.addAppender(fa_admin);
		  if(Input.CONSOLE_APPENDER)
			  this.addAppender(new ConsoleAppender(layout));
		  super.log(FQCN, PLevel.TRACE, message, null);
		  this.removeAppender(fa_admin);
	  }
  }
  
  @Override
  public void warn(Object message) {
	  if(Input.LOGGER_WARN)
	  {		  
		  if(Input.FILE_APPENDER)
			  this.addAppender(fa_admin);
		  if(Input.CONSOLE_APPENDER)
			  this.addAppender(new ConsoleAppender(layout));
		  super.log(FQCN, Level.WARN, message, null);
		  this.removeAppender(fa_manager);
	  }
  }
  
  @Override
  public void fatal(Object message) {
	  if(Input.LOGGER_FATAL)
	  {
		  if(Input.FILE_APPENDER)
			  this.addAppender(fa_admin);
		  if(Input.CONSOLE_APPENDER)
			  this.addAppender(new ConsoleAppender(layout));
		  super.log(FQCN, Level.FATAL, message, null);
		  this.removeAppender(fa_manager);
	  }
  }
  
  @Override
  public void error(Object message) {
	  if(Input.LOGGER_ERROR)
	  {
		  if(Input.FILE_APPENDER)
			  this.addAppender(fa_admin);
		  if(Input.CONSOLE_APPENDER)
			  this.addAppender(new ConsoleAppender(layout));
		  super.log(FQCN, Level.ERROR, message, null);
		  this.removeAppender(fa_manager);
	  }
  }
  
  @Override
  public void info(Object message) {
	  if(Input.LOGGER_INFO)
	  {
		  if(Input.FILE_APPENDER)
			  this.addAppender(fa_admin);
		  if(Input.CONSOLE_APPENDER)
			  this.addAppender(new ConsoleAppender(layout));
		  super.log(FQCN, Level.INFO, message, null);
		  this.removeAppender(fa_user);
	  }
  }
  
  /**
  	This method overrides {@link Logger#getLogger} by supplying
  	its own factory type as a parameter.
   */
  public static org.apache.log4j.Logger getLogger(String name) {
	  return org.apache.log4j.Logger.getLogger(name, myFactory);
	  //return org.apache.log4j.Logger.getLogger(name);
  }
  
  
}



